1 #include<iostream>
2 #include<vector>
3 #include<stdio.h>
4 #include<cstring>
5 #include<fstream>
6 #include<algorithm>

7 using
namespace std;
8 class
student
9 {
10     
public:
11         
long int reg;
12         
char name[80],branch[50];
13         
void input()
14         {
15             cout<<
"\n Enter name: ";
16             gets(name);
17             cout<<
"\n Enter roll no: ";
18             cin>>reg;
19             fflush(stdin);
20             cout<<
"\n Enter Branch: ";
21             gets(branch);
22         }
23         
void display()
24         {
25             system(
"CLS");
26             cout<<
"\t\tDisplay Records";
27             cout<<
"\n";
28             cout<<
"\n Name - "<<name;
29             cout<<
"\n Reg no. - "<<reg;
30             cout<<
"\n Branch - "<<branch;
31             cout<<
"\n";
32             system(
"PAUSE");
33             system(
"CLS");
34         }
35         
bool operator == (student a)
36         {
37             
if(reg==a.reg)
38                 
return true;
39             
else
40                 
return false;
41         }
42 };
43 vector <student>v;

44 int
search_reg(long int reg,int &i);
45 void
get_file()
46 {
47     student x;
48     
int i=0;
49     fstream f;
50     f.open(
"College.txt",ios::in);
51     
if(f)
52     {
53         f.read((
char *) &x,sizeof(class student));
54         
while(!f.eof())
55         {
56             v.push_back(x);
57             f.read((
char *) &x,sizeof(class student));
58         }
59     }
60     
else
61         ;
62     f.close();
63 }

64 void
bubblesort()
65 {
66     
int i,j;
67     student x;
68     
for(i=0;i<v.size();i++)
69         
for(j=0;j<v.size()-i-1;j++)
70             
if(v[j].reg>v[j+1].reg)
71             {
72                 x=v[j];
73                 v[j]=v[j+
1];
74                 v[j+
1]=x;
75             }
76 }

77 void
insert_new()
78 {
79     
char ch='y';
80     
int ta;
81     
while(ch=='y')
82     {
83         fflush(stdin);
84         student x;
85         x.input();
86         
if(search_reg(x.reg,ta)==0)
87             v.push_back(x);
88         
else
89             cout<<
"\nTHE REGISTRATION NO. ALREADY EXIST!!!\nCANNOT ADD";
90         cout<<
"\n Press [Y] to enter more: ";
91         cin>>ch;
92         fflush(stdin);
93     }
94 }

95 void
write_file()
96 {
97     bubblesort();
98     fstream f;
99     f.open(
"College.txt",ios::out);
100     
for(int i=0;i<v.size();i++)
101     {
102         student x=v[i];
103         f.write((
char *) &x,sizeof(class student));
104     }
105     f.close();
106 }

107 int
search_reg(long int reg,int &i)
108 {
109     
int ta=0;
110     
for(i=0;i<v.size();i++)
111         
if(v[i].reg==reg)
112         {
113             ta=
1;
114             
break;
115         }
116     
return ta;
117 }

118 int
search_name(char name[],vector<int> &vi)
119 {
120     
int i,ta=0;
121     
for(i=0;i<v.size();i++)
122         
if(strcmp(v[i].name,name)==0)
123         {
124             ta=
1;
125             vi.push_back(i);
126         }
127     
return ta;
128 }

129 int
search_branch(char branch[],vector<int> &vj)
130 {
131     
int i,ta=0;
132     
for(i=0;i<v.size();i++)
133         
if(strcmp(v[i].branch,branch)==0)
134         {
135             ta=
1;
136             vj.push_back(i);
137         }
138     
return ta;
139 }

140 void
search_and_show()
141 {
142     
int ch,i,ta=0;
143     
char name[80],branch[50];
144     vector <
int>vi;
145     vector <
int>vj;
146     
long int reg;
147     poi:
148     cout<<
"\n1.Press to search reg no."
149     <<
"\n2.Press to search name"
150     <<
"\n3.Press to search branch";
151     cin>>ch;
152     
switch(ch)
153     {
154         
case 1:
155             cout<<
"\nEnter reg no.: ";
156             cin>>reg;
157             
if(search_reg(reg,i)==1)
158                 v[i].display();
159             
else
160                 cout<<
"\nRecord NOT FOUND!!!";
161             
break;
162         
case 2:
163             cout<<
"\nEnter name: ";
164             fflush(stdin);
165             gets(name);
166             
if(search_name(name,vi)==1)
167             {
168                 
for(int j=0;j<vi.size();j++)
169                     v[vi[j]].display();
170             }
171             
else
172                 cout<<
"\nRecord NOT FOUND!!!";
173             
break;
174         
case 3:
175             cout<<
"\nEnter branch: ";
176             fflush(stdin);
177             gets(branch);
178             
if(search_branch(branch,vj)==1)
179             {
180                 
for(int j=0;j<vj.size();j++)
181                     v[vj[j]].display();
182             }
183             
else
184                 cout<<
"\nRecord NOT FOUND!!!";
185             
break;
186         
default:
187             cout<<
"\nWrong CHOICE!!!";
188             
goto poi;
189     }
190 }

191 void
show()
192 {
193     
int i;
194     
for(i=0;i<v.size();i++)
195         v[i].display();
196 }

197 void
delete_data()
198 {
199     
int i,j;
200     
long int reg;
201     vector <student>::iterator p=v.begin();
202     cout<<
"\nEnter Reg. no.: ";
203     cin>>reg;
204     
if(search_reg(reg,i)==1)
205     {
206         student x=v[i];
207         cout<<
"\nThe following data is being deleted";
208         x.display();
209         p+=i;
210         v.erase(p,p+
1);
211     }
212 }

213 void
edit_data()
214 {
215     
int i,j;
216     
long int reg;
217     vector <student>vi;
218     cout<<
"\nEnter Reg. no.: ";
219     cin>>reg;
220     
if(search_reg(reg,i)==1)
221     {
222         cout<<
"\nEnter new data:";
223         fflush(stdin);
224         v[i].input();
225     }
226 }

227 int
main()
228 {
229     
int i=1;
230     get_file();
231     
while(i)
232     {
233         system(
"CLS");
234         cout<<
"\t\t\t-----------------------------------------\n";
235         cout<<
"\t\t\t Simple College Management System\n";
236         cout<<
"\t\t\t-----------------------------------------\n";
237         cout<<
"\n\t\t\tEnter <1> to Add new student"
238             <<
"\n\t\t\tEnter <2> to Display all student"
239             <<
"\n\t\t\tEnter <3> to Remove student"
240             <<
"\n\t\t\tEnter <4> to Edit student"
241             <<
"\n\t\t\tEnter <5> to Search student"
242             <<
"\n\t\t\tEnter <0> to Exit\n";
243         cout<<
"\n\n\t\t\tEnter Your Choice:";
244         cin>>i;
245         
switch(i)
246         {
247             
case 1 :
248                 insert_new();
249                 
break;
250             
case 2 :
251                 show();
252                 
break;
253             
case 3 :
254                 delete_data();
255                 
break;
256             
case 4 :
257                 edit_data();
258                 
break;
259             
case 5 :
260                 search_and_show();
261                 
break;
262             
case 0 :
263                 write_file();
264                 
break;
265             
default :
266                 cout<<
"\nWRONG CHOICE!!!\nTRY AGAIN";
267         }
268     }
269     
return 0;
270 }


Gõ tìm kiếm nhanh...